# -*- coding:utf8 -*-

from channels import Group
from channels.handler import AsgiHandler
from django.http import HttpResponse
from channels.auth import channel_session_user, channel_session_user_from_http
from channels.sessions import channel_session
import json
from socketTwo.conf import all_conf

###########################################
# def http_consumer(message):
#     response = HttpResponse(pp.pformat(message.content), content_type='text/plain')
#     for chunk in AsgiHandler.encode_response(response):
#         logger.debug(chunk)
#         message.reply_channel.send(chunk)

#一个管道大概会持续30s
#当连接上时，发回去一个connect字符串
def ws_connect(message):
    message.reply_channel.send({'accept': True})
    Group(all_conf.GROUP_NAME).add(message.reply_channel)
    Group(all_conf.GROUP_NAME).send({'text': "WELCOME HOME!"})
    # Group(zabbix_conf.GROUP_NAME).add(message.reply_channel)

#将发来的信息原样返回
def ws_receive(message):
    data = message['text']
    Group(all_conf.GROUP_NAME).send({'text': data})


#客户端发起断开连接请求，服务端发送一个disconnect字符串，当然，此时客户端已经收不到了
def ws_disconnect(message):
    Group(all_conf.GROUP_NAME).discard(message.reply_channel)